import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import axios from 'axios'


Vue.prototype.$http = axios
axios.defaults.baseURL = '/api'

Vue.config.productionTip = false

/*
// 在request拦截器中展示进度条,NProgress.start()
axios.interceptors.request.use(
    (config) => {
        const token = JSON.parse(window.sessionStorage.getItem("token"));
        console.log("token:" + token);
        // 判断是否存在token，如果存在的话，则每个http header都加上token
        if (token) config.headers.token = token; //请求头加上token
        Nprogress.start();
        /!* ElementUI.Loading.service({
          text: "努力加载中🥶...",
          background: "rgba(0, 0, 0, 0)",
        }); *!/
        return config;
    },
    (error) => {
        return Promise.error(error);
    }
);

// 在response拦截器中隐藏进度条,NProgress.done()
axios.interceptors.response.use(
    (response) => {
        if (response.data.code === 401) {
            ElementUI.Message({
                message: "当前登录已失效，请重新登录",
                center: true,
                type: "error",
            });
            window.sessionStorage.removeItem("token");
            router.push("login");
        }
        Nprogress.done();
        /!*  Vue.nextTick(() => {
          // 以服务的方式调用的 Loading 需要异步关闭
          ElementUI.Loading.service().close();
        }); *!/
        return response;
    },
    (error) => {
        return Promise.error(error);
    }
);
*/

new Vue({
    router,
    render: h => h(App)
}).$mount('#app')

